package com.tiwi.gmbdr.OpenHelper;

import java.util.List;

import com.tiwi.gmbdr.constant.DBTablesSql;
import com.tiwi.gmbdr.constant.Global;
import com.tiwi.gmbdr.model.Question;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class QuestionOpenHelper extends SQLiteOpenHelper {

	public static final String TABLE_NAME = "question";
	
	
	public static final String NO_SOAL = "no_soal";
	public static final String SOAL = "soal";
	public static final String GAMBAR = "gambar";
	public static final String JAWABAN_A = "jawaban_a";
	public static final String JAWABAN_B = "jawaban_b";
	public static final String JAWABAN_C = "jawaban_c";
	public static final String JAWABAN_D = "jawaban_d";
	public static final String JAWABAN_E = "jawaban_e";
	public static final String KUNCI_JAWABAN = "kunci_jawaban";
	Context context = null;
	
	public QuestionOpenHelper(Context context, String name,
			CursorFactory factory, int version) {
		super(context, Global.DATABASE_NAME, null, Global.DATABASE_VERSION);		
		this.context = context;
	}
	
	public QuestionOpenHelper(Context context) {
		super(context, Global.DATABASE_NAME, null, Global.DATABASE_VERSION);		
		this.context = context;
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		List<String> tableSqlList = DBTablesSql.getAllTableStructureSql();
		for (String sql : tableSqlList) {
			db.execSQL(sql);
		}

	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub

	}
	
	//digunakan untuk menginputkan pertanyaan
	public void insertQuestion(Question bean){
		SQLiteDatabase db = getWritableDatabase();
		ContentValues values = aplicationToContentValues(bean);
		
		
		try {
			
				System.out.println("insertOrThrow");
				db.insertOrThrow(TABLE_NAME, null, values);	
			
				
		}catch (Exception e) {
			System.out.println("insertQuestion Error "+e);
		}
		finally {
			if (db.isOpen())
				db.close();
		}
		
		
	}
	
	public ContentValues aplicationToContentValues(Question bean){
	
		
		ContentValues cValues = new ContentValues();
		
		cValues.put(NO_SOAL, bean.getNo_soal());
		cValues.put(SOAL, bean.getSoal());
		cValues.put(GAMBAR, bean.getGambar());
		cValues.put(JAWABAN_A, bean.getJawaban_a());
		cValues.put(JAWABAN_B, bean.getJawaban_b());
		cValues.put(JAWABAN_C, bean.getJawaban_c());
		cValues.put(JAWABAN_D, bean.getJawaban_d());
		cValues.put(JAWABAN_E, bean.getJawaban_e());
		cValues.put(KUNCI_JAWABAN, bean.getKunci_jawaban());
	
		
		return cValues;
	}
	
	//digunakan untuk mengambil soal dari database
	public Question getOneQuestions(String id){
Question result = null;
		
		StringBuilder where = new StringBuilder()
		.append(NO_SOAL).append(" = ?");
			final String delimiter = "||';'||";
			final String ifNullOpen = "IFNULL(";
			final String ifNullClose = ",'')";
			
			
			StringBuilder sql = new StringBuilder().append("SELECT ")
			.append(NO_SOAL).append(delimiter)
			.append(SOAL).append(delimiter)
			.append(ifNullOpen).append(KUNCI_JAWABAN).append(ifNullClose).append(delimiter)
			.append(ifNullOpen).append(JAWABAN_A).append(ifNullClose).append(delimiter)
			.append(ifNullOpen).append(JAWABAN_B).append(ifNullClose).append(delimiter)
			.append(ifNullOpen).append(JAWABAN_C).append(ifNullClose).append(delimiter)
			.append(ifNullOpen).append(JAWABAN_D).append(ifNullClose).append(delimiter)
			.append(ifNullOpen).append(JAWABAN_E).append(ifNullClose)
			.append(" FROM ").append(TABLE_NAME)
			.append(" WHERE ").append(where);
		String[] selectionArgs = {id};
		
		SQLiteDatabase db = getReadableDatabase();
		Cursor cursor = db.rawQuery(sql.toString(), selectionArgs);
		try {

			while (cursor.moveToNext()) {
				if (result == null)
					result  = new Question(cursor.getString(0));
			}
		}
		finally {
			if (cursor != null && !cursor.isClosed())
				cursor.close();
			if (db.isOpen())
				db.close();
		}
		
		return result;

		
		
	}

}
